//
//  XOHRegisterVC.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/9/29.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class XOHRegisterVC: UIViewController {
    
    @IBOutlet weak var usernameTF: UITextField!
    @IBOutlet weak var usernameHintLB: UILabel!
    
    @IBOutlet weak var codeTF: UITextField!
    @IBOutlet weak var codeHintLB: UILabel!
    @IBOutlet weak var codeBT: UIButton!
    
    @IBOutlet weak var passwordTF: UITextField!
    @IBOutlet weak var passwordHintLB: UILabel!
    
    @IBOutlet weak var registerBT: UIButton!
    
    let disposeBag = DisposeBag()
    
    private var vm: XOHRegisterVM!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        vm = XOHRegisterVM(username: usernameTF.rx.text.orEmpty.asObservable(),
                           code: codeTF.rx.text.orEmpty.asObservable(),
                           password: passwordTF.rx.text.orEmpty.asObservable())
        
//        vm.usernameValid.bind(to: passwordTF.rx.isEnabled).disposed(by: disposeBag)
        vm.usernamErrorHintValid.bind(to: usernameHintLB.rx.isHidden).disposed(by: disposeBag)
        vm.usernameValid.bind(to: codeBT.rx.isEnabled).disposed(by: disposeBag)
        
        vm.codeErrorHintValid.bind(to: codeHintLB.rx.isHidden).disposed(by: disposeBag)
        
        vm.passwordErrorHintValid.bind(to: passwordHintLB.rx.isHidden).disposed(by: disposeBag)
//        vm.passwordValid.bind(to: passwordHintLB.rx.isHidden).disposed(by: disposeBag)
        
        vm.everythingValid.bind(to: registerBT.rx.isEnabled).disposed(by: disposeBag)
        
        registerBT.rx.tap.subscribe(onNext: { [weak self] in
            self?.vm.sumbit()
        }).disposed(by: disposeBag)
        
//        codeBT.rx.tap.subscribe(onNext: { [weak self] in
//            self?.codeBT.countDown(59, disposeBag: self?.disposeBag)
//        }).disposed(by: disposeBag)
        
       let timer = Observable<Int>.timer(.seconds(0), period: .seconds(1), scheduler: MainScheduler.instance)
                         .map{59 - $0}
                         .filter{ $0 >= 0 }
                         .asDriver(onErrorJustReturn: 0)

       let second = codeBT.rx.tap
           .flatMapLatest { _ -> Driver<Int> in
               return timer
       }
       
       let sendCodeButtonText = second.map { [weak self] (e) -> String in
           self?.codeBT.isEnabled = (e == 0 ? true : false)
           return (e == 0 ? "发送验证码":"\(e)s")
       }
       
       sendCodeButtonText.bind(to: codeBT.rx.title(for: .normal)).disposed(by: disposeBag)
        
        
        Observable<Int>.timer(.seconds(5), period: .seconds(2), scheduler: MainScheduler.instance)
            .subscribe { (event) in
                print(event)
            }
        .disposed(by: disposeBag)
        
        // Do any additional setup after loading the view.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

//extension UIButton {
//
//    func countDown(_ total: Int) {
//        let timer = Observable<Int>.timer(.seconds(0), period: .seconds(1), scheduler: MainScheduler.instance)
//                        .map{total - $0}
//                        .filter{ $0 >= 0 }
//                        .asDriver(onErrorJustReturn: 0)
//
//    }
//
//}
